package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.common.utils.DateUtils;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.framework.manager.AsyncManager;
import com.lechi.web.service.ICmsMainService;
import com.lechi.web.system.domain.CmsMain;
import com.lechi.web.system.mapper.iot.CmsMainMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 官网文章发布管理Service业务层处理
 *
 * @author yangguang
 */
@RequiredArgsConstructor
@Service
public class CmsMainServiceImpl implements ICmsMainService {

    private final CmsMainMapper cmsMainMapper;

    /**
     * 查询官网文章发布管理
     *
     * @param id 官网文章发布管理主键
     * @return 官网文章发布管理
     */
    @Override
    public CmsMain selectCmsMainById(Long id) {
        return cmsMainMapper.selectCmsMainById(id);
    }

    /**
     * 查询官网文章发布管理列表
     *
     * @param CmsMain 官网文章发布管理
     * @return 官网文章发布管理
     */
    @Override
    public List<CmsMain> selectCmsMainList(CmsMain CmsMain) {
        try {
            return cmsMainMapper.selectCmsMainList(CmsMain);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            AsyncManager.me().execute(IotExceptionLog.exceptionLog(e, "查询官网文章列表", method, CmsMain));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 新增官网文章发布管理
     *
     * @param CmsMain 官网文章发布管理
     * @return 结果
     */
    @Override
    public int insertCmsMain(CmsMain CmsMain) {
        CmsMain.setCreateTime(DateUtils.getBaseTime());
        try {
            return cmsMainMapper.insertCmsMain(CmsMain);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            AsyncManager.me().execute(IotExceptionLog.exceptionLog(e, "新增官网文章", method, CmsMain));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 修改官网文章发布管理
     *
     * @param CmsMain 官网文章发布管理
     */
    @Override
    public void updateCmsMain(CmsMain CmsMain) {
        try {
            cmsMainMapper.updateCmsMain(CmsMain);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            AsyncManager.me().execute(IotExceptionLog.exceptionLog(e, "修改官网文章", method, CmsMain));
            throw new ServiceException(ErrorCodeEnum.EDIT_ERROR.getValue(), ErrorCodeEnum.EDIT_ERROR.getKey());
        }
    }

    /**
     * 批量删除官网文章发布管理
     *
     * @param ids 需要删除的官网文章发布管理主键
     */
    @Override
    public void deleteCmsMainByIds(Long[] ids) {
        try {
            cmsMainMapper.deleteCmsMainByIds(ids);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            AsyncManager.me().execute(IotExceptionLog.exceptionLog(e, "批量删除官网文章", method, ids));
            throw new ServiceException(ErrorCodeEnum.EDIT_ERROR.getValue(), ErrorCodeEnum.EDIT_ERROR.getKey());
        }
    }
}
